// DlgWelcom.cpp : implementation file
//

#include "stdafx.h"
#include "XIE.h"
#include "DlgWelcom.h"
#include <exdispid.h>
#include "MainFrm.h"

// CDlgWelcom dialog

IMPLEMENT_DYNCREATE(CDlgWelcom, CDHtmlDialog)

CDlgWelcom::CDlgWelcom(CWnd* pParent /*=NULL*/)
	: CDHtmlDialog(CDlgWelcom::IDD, CDlgWelcom::IDH, pParent)
{

}

CDlgWelcom::~CDlgWelcom()
{
}

void CDlgWelcom::DoDataExchange(CDataExchange* pDX)
{
	CDHtmlDialog::DoDataExchange(pDX);
}

BOOL CDlgWelcom::OnInitDialog()
{
	CDHtmlDialog::OnInitDialog();
	
	HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	SetIcon(hIcon, TRUE);
	SetIcon(hIcon, FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

BEGIN_MESSAGE_MAP(CDlgWelcom, CDHtmlDialog)
	ON_WM_CLOSE()
END_MESSAGE_MAP()

BEGIN_DHTML_EVENT_MAP(CDlgWelcom)
	DHTML_EVENT_ONCLICK(_T("ButtonOK"), OnButtonOK)
	DHTML_EVENT_ONCLICK(_T("ButtonCancel"), OnButtonCancel)
	DHTML_EVENT_ONCLICK(_T("lnkSetAccount"), OnButtonSetAccount)
END_DHTML_EVENT_MAP()


BEGIN_EVENTSINK_MAP(CDlgWelcom, CDHtmlDialog)
	ON_EVENT(CDlgWelcom, AFX_IDC_BROWSER, DISPID_NEWWINDOW2, OnNewWindow2, VTS_PDISPATCH VTS_PBOOL)
END_EVENTSINK_MAP()


// CDlgWelcom message handlers

HRESULT CDlgWelcom::OnButtonOK(IHTMLElement* /*pElement*/)
{
	SaveProfile();
	OnOK();
	return S_OK;
}

HRESULT CDlgWelcom::OnButtonSetAccount(IHTMLElement* /*pElement*/)
{
	((CMainFrame*)(AfxGetMainWnd()))->OnSettingUser();
	return S_OK;
}

HRESULT CDlgWelcom::OnButtonCancel(IHTMLElement* /*pElement*/)
{
	SaveProfile();
	OnCancel();
	return S_OK;
}

void CDlgWelcom::OnDocumentComplete(LPDISPATCH pDisp, LPCTSTR szUrl)
{
	CDHtmlDialog::OnDocumentComplete(pDisp, szUrl);

	if (m_strCurrentUrl == szUrl)
	{
		CComPtr<IHTMLElement> pHtmlElement;
		CComPtr<IHTMLInputElement> pInput;	

		VARIANT_BOOL vCheck;+
		GetElement(_T("chkShowAlways"), &pHtmlElement);

		if (pHtmlElement)
		{
			pHtmlElement->QueryInterface(IID_IHTMLInputElement, (void**)&pInput);
			if (pInput)
			{
				
				int n =	AfxGetApp()->GetProfileInt(_T("settings"), _T("ShowWelcome"), 1);
				vCheck = n;
				pInput->put_checked(vCheck);				
			}		
		}	
	}
	
	// TODO: Add your specialized code here and/or call the base class
}

void CDlgWelcom::OnNewWindow2(LPDISPATCH* ppDisp, BOOL* Cancel)
{
	//*Cancel = FALSE;
	CMainFrame* pWndMainfram = (CMainFrame*)AfxGetMainWnd();

	if (pWndMainfram)
	{
		CXIEView* pIE = pWndMainfram->NewIEView(FALSE);
		if (pIE)
		{
			pIE->SetRegisterAsBrowser(TRUE);   
			*ppDisp = pIE->GetApplication();	
		}
	}
}
void CDlgWelcom::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	SaveProfile();
	ShowWindow(SW_HIDE);
	//CDHtmlDialog::OnClose();
}

void CDlgWelcom::SaveProfile()
{
	CComPtr<IHTMLElement> pHtmlElement;
	CComPtr<IHTMLInputElement> pInput;	

	VARIANT_BOOL vCheck;

	GetElement(_T("chkShowAlways"), &pHtmlElement);

	if (pHtmlElement)
	{
		pHtmlElement->QueryInterface(IID_IHTMLInputElement, (void**)&pInput);
		if (pInput)
		{
			pInput->get_checked(&vCheck);
			int n = vCheck;
			AfxGetApp()->WriteProfileInt(_T("settings"), _T("ShowWelcome"), n);
		}		
	}
}

